﻿
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SuperGo.SilverlightLib;
using SuperGo.SilverlightLib.Sgf;

namespace SuperGo.TestSilverlightLib.Sgf {
    [TestClass]
    public class SgfReaderTest {
        [TestMethod]
        public void TestReadEmptyBoard() {
            SgfReader reader = new SgfReader();
            TextReader textReader = new StringReader(@"
(;GM[1]FF[4]AP[glGo:1.4]ST[1]

SZ[9]KM[6.5]
PW[White]PB[Black]DT[2008-06-03]
)
");
            GoGame game = reader.Read(textReader);
            const string expected =
@" ABCDEFGHJ
 1 .........
 2 .........
 3 .........
 4 .........
 5 ......... 
 6 ......... 
 7 ......... 
 8 ......... 
 9 ......... ";
            string found = game.Board.ToString();
            Assert.AreEqual(FixBoard(expected), FixBoard(found));
        }

        [TestMethod]
        public void TestReadSimple() {
            SgfReader reader = new SgfReader();
            TextReader textReader = new StringReader(@"
(;GM[1]FF[4]AP[glGo:1.4]ST[1]

SZ[9]KM[6.5]
PW[White]PB[Black]DT[2008-06-03]

;B[bb]
;W[cb]
;B[cc]
;W[bc]
;B[tt]
;W[tt])
");
            GoGame game = reader.Read(textReader);
            const string expected =
@" ABCDEFGHJ
 1 .........
 2 .XO......
 3 .OX......
 4 .........
 5 ......... 
 6 ......... 
 7 ......... 
 8 ......... 
 9 ......... ";
            string found = game.Board.ToString();
            Assert.AreEqual(FixBoard(expected), FixBoard(found));
        }

        [TestMethod]
        public void TestReadWithTree() {
            SgfReader reader = new SgfReader();
            TextReader textReader = new StringReader(@"
(;GM[1]FF[4]AP[glGo:1.4]ST[1]

SZ[9]KM[6.5]
PW[White]PB[Black]DT[2008-06-04]

;B[bb]
;W[cb]
(;B[cc];W[bc])
(;B[db];W[eb]))
");
            GoGame game = reader.Read(textReader);
            const string expected =
@" ABCDEFGHJ
 1 .........
 2 .XO......
 3 .........
 4 .........
 5 ......... 
 6 ......... 
 7 ......... 
 8 ......... 
 9 ......... ";
            string found = game.Board.ToString();
            Assert.AreEqual(FixBoard(expected), FixBoard(found));
        }

        [TestMethod]
        public void TestReadWithHandicap() {
            SgfReader reader = new SgfReader();
            TextReader textReader = new StringReader(@"
(;GM[1]FF[4]AP[glGo:1.4]ST[1]

SZ[9]HA[3]KM[6.5]
PW[White]PB[Black]DT[2008-06-04]
AB[cc][gc][cg])
");
            GoGame game = reader.Read(textReader);
            const string expected =
@" ABCDEFGHJ
 1 .........
 2 .........
 3 ..X...X..
 4 .........
 5 ......... 
 6 ......... 
 7 ..X...... 
 8 ......... 
 9 ......... ";
            string found = game.Board.ToString();
            Assert.AreEqual(FixBoard(expected), FixBoard(found));
        }

        [TestMethod]
        public void TestReadWithCaptures() {
            SgfReader reader = new SgfReader();
            TextReader textReader = new StringReader(@"
(;GM[1]FF[4]AP[glGo:1.4]ST[1]

SZ[9]KM[0.0]
PW[White]PB[Black]RE[W+1.0]DT[2008-06-04]
AB[hi][fi][ih][hh][gh][fh][aa]
AW[ab][bb][ca]
TB[gi][ii]
TW[aa][ba]
C[White wins by 1.0 points.])
");
            GoGame game = reader.Read(textReader);
            const string expected =
@" ABCDEFGHJ
 1 ..O......
 2 OO.......
 3 .........
 4 .........
 5 ......... 
 6 ......... 
 7 ......... 
 8 .....XXXX 
 9 .....X.X. ";
            string found = game.Board.ToString();
            Assert.AreEqual(FixBoard(expected), FixBoard(found));
        }

        private static string FixBoard(string boardString) {
            boardString = boardString.Replace("\r", " ");
            boardString = boardString.Replace("\n", " ");
            boardString = Regex.Replace(boardString, @"\s+", " ");
            boardString = boardString.Trim();
            return boardString;
        }
    }
}
